package zaehler;


	
	import java.awt.BorderLayout;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import java.awt.event.WindowEvent;

	import javax.swing.JButton;
	import javax.swing.JFrame;
	import javax.swing.JLabel;
	import javax.swing.SwingConstants;


	
	/**
	 *<code> Zaehler <code> is a plus-minus-calculator. Starts with the initial value zero. <br/>
	 *Presses the button plus, the value rises plus one.<br />
	 *Presses the button minus, the value decreases minus one. Includes an Exit-button.<br/>
	 *  @author Michaela Maierhofer
	 *  @version 1.0
	 *  
	 */


	public class Zaehler extends JFrame implements ActionListener {

		
		/**
		 * shows the text "Plus-Minus-Zaehler" as a Label
		 *
		 */
		private JLabel lblText = new JLabel("Plus-Minus-Zaehler");
		
		/**
		 * shows a "+" button
		 */
		private JButton btnPlus = new JButton("+");
		
		/**
		 * starts count with initial value zero
		 */
		private int cnt = 0; 
		
		/**
		 * shows a "-" button
		 */
		private JButton btnMinus = new JButton("-");
		
		/**
		 * shows an "Exit"-Button
		 */
		private JButton btnExit = new JButton("Exit");
		
		/**
		 * shows the text "0" as a Label
		 */
		private JLabel lblCnt = new JLabel("0");

		
		
		
		/**
		 * <code>Main</code>-Method creates a window for the plus-minus-calculator and implements a DefaultCloseOperation<br />
		 *
		 * @param args
		 * 
		 */
		public static void main(String[] args) {
			/**
			 * creates a new object wnd and makes some configuration on it
			 * @param EXIT_ON_CLOSE
			 */
			Zaehler wnd = new Zaehler();
			wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			wnd.setSize(300,300);
			wnd.setVisible(true);
			
					
		}
		
		/**
		 * Creates a Default constructor with all needed layout settings.<br/>
		 * Uses a BorderLayout.
		 */
		
		public Zaehler(){
			
			/**
			 * creates a window title 
			 * @param PLUS-Minus-Zaehler
			 */
			super("PLUS-Minus-Zaehler");
			
			//creates a BorderLayout and shows the buttons and labels
			this.getContentPane().add(lblText,BorderLayout.NORTH);
			this.getContentPane().add(btnPlus, BorderLayout.WEST);
			this.getContentPane().add(btnMinus,BorderLayout.EAST);
			this.getContentPane().add(btnExit,BorderLayout.SOUTH);
			this.getContentPane().add(lblCnt,BorderLayout.CENTER);
			lblText.setHorizontalAlignment(SwingConstants.CENTER);
			lblCnt.setHorizontalAlignment(SwingConstants.CENTER);
		
		
		
		
			//sets the ActionCommands for the buttons +,- and Exit
		 	this.btnPlus.setActionCommand("+");
			this.btnMinus.setActionCommand("-");
			this.btnExit.setActionCommand("Exit");

			//adds the ActionListener to the three button
			this.btnPlus.addActionListener(this);
			this.btnMinus.addActionListener(this);
			this.btnExit.addActionListener(this);
					}
		
		
		/**
		 *_A standard <code>windowClosed</code> method with a WindowEvent e
		 *@param e
		 */
		public void windowClosed(WindowEvent e) {
			System.exit(0);
		}
		
		
			/**
			 * implements the reaction of a button click
			 * @param e
			 */
		
			public void actionPerformed(ActionEvent e)
				{
				
			
				String action = e.getActionCommand();
				
				//if presses the "+" Button the count rises plus one and the label text shows the value of the count
				if (action.equalsIgnoreCase("+"))

				{
					this.cnt++;
					this.lblCnt.setText(" "+this.cnt+" ");
					
				}
				
				//if presses the "-" Button the count decreases minus one and the label text shows the value of the count
				 if (action.equalsIgnoreCase("-")){
					 this.cnt --;
					 this.lblCnt.setText(" "+this.cnt+" ");
					
				}
				 
				//if presses the "Exit" Button the complete window closed
				 if (action.equalsIgnoreCase("Exit")){
			         System.exit(0);
				 }
				
			
				}

			public void test(){
				
			boolean test = true;
						
			}
			

}
